//
//  RJLine.m
//  AirQuality
//
//  Created by Po on 15/4/30.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJLine.h"
#import "SHLineGraphView.h"
#import "SHPlot.h"
@interface RJLine ()

@property (retain, nonatomic) SHLineGraphView * lineView;
@property (retain, nonatomic) NSString * keyString;             //具体空气数据
@property (retain, nonatomic) NSArray * airData;                //每小时平均值
@property (assign, nonatomic) CGFloat maxHeight;

- (void)initInterface;
@end
@implementation RJLine

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data maxHeight:(CGFloat)height
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([data count] != 0) {
            if ([[[data allValues] firstObject] isKindOfClass:[NSString class]]) {
                 _keyString = [data allKeys][1];
                _airData = [NSArray arrayWithArray:[data allValues][1]];
            }else{
                _keyString = [data allKeys][0];
                _airData = [NSArray arrayWithArray:[data allValues][0]];
            }
             _maxHeight = height;
        }
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    if (_keyString) {
        [self setContentSize:CGSizeMake(1000, self.bounds.size.height)];
        [self setContentOffset:CGPointMake(0, 0)];
        [self addSubview:[self getLineView]];
        [_lineView setupTheView];
    } else {
        UILabel * label = [[UILabel alloc] initWithFrame:self.bounds];
        [label setText:@"没有获取到折线图数据"];
        [label setFont:[UIFont boldSystemFontOfSize:16]];
        [label setTextColor:[UIColor blackColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:label];
    }
}

- (SHLineGraphView *)getLineView
{
    if (!_lineView) {
        CGRect rect = CGRectZero;
        rect.size = self.contentSize;
        _lineView = [[SHLineGraphView alloc] initWithFrame:rect];
        _lineView.AirType = [RJTools getTrueTypeTitlleWithCellKey:_keyString];
        _lineView.barChartWidth = 5;
        if ([_lineView.AirType isEqualToString:@"PM2.5"]) {
            _lineView.AirType = @"PM2_5";
        }
        
        [self initLineView];
    }
    return _lineView;
}



/**初始化折线图*/
- (void)initLineView
{
    if (_airData.count == 0) {
        return;
    }
    _lineView.barChartShowBtn = NO;
    _lineView.barChartShowNum = YES;
    //线条下方填充
    _lineView.fillLineRect = NO;
    //可以设置是否显示柱状图
    _lineView.barChartAlpha = 0;
    _lineView.checkFillColor = YES;
    _lineView.fillColor = [UIColor clearColor];
    NSDictionary *_themeAttributes = @{
                                       kXAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kXAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kYAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelSideMarginsKey : @10,
                                       kPlotBackgroundLineColorKye : GetRGBColor(85, 85, 85, 0.2)
                                       };
    _lineView.themeAttributes = _themeAttributes;
    _lineView.yAxisRange = @(_maxHeight);
    
    
    
    _lineView.yAxisSuffix = @"ug";

    //X轴时间
    NSMutableArray * xLabelArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 1; i <= 24; i ++) {
        [xLabelArray addObject:@{@(i):[NSString stringWithFormat:@"%ld时",(unsigned long)i]}];
    }
    _lineView.xAxisValues = xLabelArray;
    //转化数据为折线图数据
    NSArray * dataArray = [self updateData:_airData];
    for (NSUInteger i = 0; i < dataArray.count; i ++) {
        NSArray * lineData = dataArray[i];
        SHPlot *_plot1 = [[SHPlot alloc] init];
        //转化数据
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        for (NSUInteger i = 0; i < 24; i ++) {
            [dataArray addObject:@{@(i+1):lineData[i]}];
        }
        _plot1.plottingValues = dataArray;
        
        NSMutableArray * labelsTitle = [NSMutableArray arrayWithCapacity:0];
        for (NSNumber * number in lineData) {
            if ([_keyString isEqualToString:@"CO"]) {
                [labelsTitle addObject:[NSString stringWithFormat:@"%.2f",[number floatValue]]];
            } else {
                [labelsTitle addObject:[NSString stringWithFormat:@"%ld",[number integerValue]]];
            }
        }
        _plot1.plottingPointsLabels = labelsTitle;
        
        NSDictionary *_plotThemeAttributes = @{
                                               kPlotFillColorKey : [RJTools getLineColors][i],
                                               kPlotStrokeWidthKey : @2,
                                               kPlotStrokeColorKey : [RJTools getLineColors][i],
                                               kPlotPointFillColorKey : [RJTools getLineColors][i],
                                               kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                               };
        //线条颜色
        _plot1.plotThemeAttributes = _plotThemeAttributes;
        //加入线条
        [_lineView addPlot:_plot1];
    }

}

/**更新数据*/
- (NSArray *)updateData:(NSArray *)data
{
    NSMutableArray * tempData = [NSMutableArray array];
    for (NSDictionary * dic in data) {
        [tempData addObject:[[dic allValues] lastObject]];
    }
    return tempData;
}

@end
